package smetana.modules.ui.form.search;

import com.google.inject.Inject;
import org.apache.pivot.collections.List;
import org.apache.pivot.wtk.Dialog;
import smetana.domain.raw.Agent;
import smetana.modules.business.BusinessAgent;
import smetana.modules.business.BusinessPivot;

/**
 *
 * @author dmitry.mamonov
 */
public class SearchAgentDialog extends AbstractSearchPanel<Agent> {
    Dialog dialog;

    @Inject
    BusinessAgent businessAgent;

    @Inject
    BusinessPivot businessPivot;

    public SearchAgentDialog() {
        super();
    }

    @Override
    protected Agent createNew(String text) {
        return businessAgent.newAgent(text);
    }

    @Override
    protected void edit(Agent current, String newText) {
        Agent agent = businessAgent.findAgentById(current.getId());
        agent.setAgentName(newText);
        businessAgent.saveAgent(agent);
    }

    @Override
    protected List<Agent> load() {
        return businessPivot.pivotListOfAgents();
    }

    @Override
    protected String toString(Agent value) {
        return value.getAgentName();
    }

    @Override
    protected void performSelect() {
        dialog.close(getResult()!=null);
    }

    @Override
    public void init() {
        super.init();
        dialog = new Dialog("Выбор заказчика", this.getComponent());
    }

    public Dialog getDialog() {
        initialize();
        return dialog;
    }
}
